package dm.ast;

import test.envset.GlobalState;

/**
 * DM的集函数
 * 一般用于列查询表达式
 */
public class DmAggregateFun extends DmExpression{
    //expLeft作为参数
    //DmASTNode expLeft = null; //左操作数
    private DmAggregateType aggFuncName = null; //集函数名称
    private String Grouping = null;  //GROUPING函数
    private boolean starFlag = false; //startFlag True means count(*)
    private boolean distinctFlag = false;  //distinctFlag True means count(distinct c1) , sum(distinct c1)...

    @Override
    public void setWidt(){
        wid_t = 1;
        if(expLeft != null){
            wid_t = expLeft.wid_t;
        }
    }

    @Override
    public void setDept(){
        dep_t = 1;
        if(expLeft != null){
            dep_t = expLeft.dep_t;
        }
    }

    public boolean getStarFlag() {
        return this.starFlag;
    }

    public void setStarFlag(boolean starFlag) {
        this.starFlag = starFlag;
    }

    public boolean getDistinctFlag() {
        return this.distinctFlag;
    }

    public void setDistinctFlag(boolean distinctFlag) {
        this.distinctFlag = distinctFlag;
    }

    //设置集函数返回类型
    public DmAggregateType getAggregateType() {
        return this.aggFuncName;
    }
    //设置函数类型
    public void setAggFuncType(DmAggregateType aggType) {
        this.aggFuncName = aggType;
    }
    //GROUPING函数类型
    public void setGrouping(String string) {
        this.Grouping = string;
    }

    //设置集函数返回类型
    public void setDmDataType(DmDataType reType){
        this.reType = reType;
    }

    @Override
    public String toSql(){
        if(aggFuncName == null && Grouping == null){
            return " " + this.getClass().getSimpleName() + getSqlTarget();
        }else {
            StringBuilder sql = new StringBuilder();
            sql.append(" ");
            if(Grouping != null){
                sql.append(Grouping.toString());
            }else {
                sql.append(aggFuncName.toString());
            }
            sql.append("(");
            if(distinctFlag){
                sql.append("DISTINCT ");
            }
            if(starFlag){
                sql.append("*");
            }else {
                sql.append(expLeft.toSql());
            }
            sql.append(")");
            return sql.toString();
        }

    }

    @Override
    public void fill(GlobalState globalState){
        if(starFlag){
            isCompleted = true;
        }
        if(!isCompleted){
            expLeft.fill(globalState);
            isCompleted = true;
        }
    }

    public void setParameter(DmExpression exp){
        setLeftExp(exp);
        if(reType == null){
            reType = exp.reType;
        }
    }

    public DmASTNode getParameter(){
        return getLeftExp();
    }

    //构造集函数
    public static DmExpression buildDmAggregate(DmAggregateType aggFuncName , DmExpression parameter){
        DmAggregateFun dmAgg = new DmAggregateFun();
        dmAgg.setAggFuncType(aggFuncName);
        if(aggFuncName.reType != null){
            dmAgg.reType = aggFuncName.reType;
        }else {
            //函数返回类型为null，表示根据参数类型确定返回类型。例如：max、min
            dmAgg.reType = parameter.reType;
        }

        dmAgg.setParameter(parameter);
        dmAgg.isCompleted = parameter.isCompleted;
        return dmAgg;
    }

    //构造一个grouping函数
    public static DmExpression Grouping(DmExpression parameter){
        DmAggregateFun dmAgg1 = new DmAggregateFun();
        dmAgg1.reType = DmDataType.INT;
        dmAgg1.setGrouping("GROUPING");
        dmAgg1.setParameter(parameter);
        dmAgg1.isCompleted = parameter.isCompleted;
        return dmAgg1;
    }
}
